//Before this is 495 lines
package org.tds.master;


import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EventDetected extends Activity {
	public int maxTime = 60;
	public int curTime = maxTime;
	public final String TAG = "TDS-EventDetected";
	private TextView txtStatus;
	private RefreshHandler updater = new RefreshHandler();
	public Button confirm, cancel, reset;
	
	public double critSpeed = 4.5; //10mph
	private LocationManager lm;
	public LocationListener list = null;
	public boolean fin = false;
	public boolean midAlert = false;
	boolean hasSpeed; 
	
	@Override 
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle); 
		setContentView(R.layout.detected_main);
		Log.d(TAG,"Created");
		this.txtStatus = (TextView) this.findViewById(R.id.timeLeft);

		confirm = (Button) findViewById(R.id.confirm);
		cancel = (Button) findViewById(R.id.cancel);	
		reset = (Button) findViewById(R.id.reset);	
		
		reset.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				voidButtons();
				reset();
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (midAlert)
					canceled();
			}
		});
		confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (midAlert)
					confirmed();
			}
		});

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    	Criteria crit = new Criteria();
    	crit.setSpeedRequired(true);
    	list = new locList();
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, list );

		doCountdown();		  	
	} 
	
	  private class locList implements LocationListener {
			@Override
			public void onLocationChanged(Location loc) {
				Log.d(TAG,"loc changed");
				if (loc.hasSpeed()) {
					hasSpeed = true;
					if (loc.getSpeed() > critSpeed) {
						canceled();
						lm.removeUpdates(this);
					}
				}else{
					hasSpeed = false;
					Log.w(TAG,"No speed data");
				}
			}
			@Override
			public void onProviderDisabled(String provider) {}
			@Override
			public void onProviderEnabled(String provider) {}
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {}
	    }
	  
	  public boolean onKeyDown(int keyCode, KeyEvent event) {
		  if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			  if (!fin && midAlert)
				  canceled();
			  return true;
		  }else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			  if (!fin && midAlert)
				  confirmed();
			  return true;
		  }else	  
			  return super.onKeyDown(keyCode, event);
	  }
	
	private void doCountdown() { 		
		curTime--;
		
		if(curTime > 0){
			updater.sleep(1000);
			
			if (curTime < (maxTime/2) && !midAlert) {
				midAlert = true;
			}else if (curTime < 30) {
				txtStatus.setText("Time left: "+curTime);
			}
		}else{
			noResponse();
		}
	}
	
	class RefreshHandler extends Handler {
		public void sleep(long delayMillis) {
			this.removeMessages(0);
			sendMessageDelayed(obtainMessage(0), delayMillis);
		}
	 	
		@Override
		public void handleMessage(Message msg) {
			EventDetected.this.doCountdown();
		}
	};
	
	public void voidButtons() {
		confirm.setEnabled(false);
		cancel.setEnabled(false);
		fin = true;
	}
	
	public void reset() {
		confirm.setEnabled(true);
		cancel.setEnabled(true);		
		fin = false;
		curTime = maxTime; 
		midAlert = false;
		txtStatus.setText("Checking for location change");
		doCountdown();
	}
		
	public void confirmed() {
		txtStatus.setText("Confirmed!");
		updater.removeMessages(0);
		voidButtons();

	}
	
	public void canceled() {
		txtStatus.setText("Aborted");
		updater.removeMessages(0);
		voidButtons();
	}

	public void noResponse() {
		txtStatus.setText("Time's up!");
		updater.removeMessages(0);
		//hasSpeed should be sent to sending messages since without it it's unlikely to be legit.
		voidButtons();
	}

	public void onPause() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		lm.removeUpdates(list);
		updater.removeMessages(0);
		super.onPause();
	}	
	
	@Override
	public void onDestroy() {
		Log.i(TAG,"Event Detected destroyed");
		super.onDestroy();
	}
	
}
